package obs;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ManyThreadBlocking {
    public static void debug(Object o){
        String s = ""+Thread.currentThread().getName();

        System.out.println(s+" thread: "+o.toString());
    }

    public static SyncMeiMei syncMeiMei = new SyncMeiMei();
    public static SyncXiaoMing syncXiaoMing = new SyncXiaoMing();
    public static ReentrantLock meimeilock = new ReentrantLock();
    public static Condition meimeitag = meimeilock.newCondition();
    public static ReentrantLock dodonelock = new ReentrantLock();
    public static Condition dodonetag = dodonelock.newCondition();
    public static void main(String[] args) throws InterruptedException {

        new Thread(()-> {

            Scanner scanner = new Scanner(System.in);
            while (true){
                String s = scanner.nextLine();
                Integer r= 0;
                try {
                    r = Integer.parseInt(s);
                }catch (Exception e){
                    r=100;
                }
                meimeilock.lock();
                try{
                    // 事件
                    syncMeiMei.setAge(r);

                    debug("妹妹年龄　"+syncMeiMei.getAge());
                    //通知notify
                    meimeitag.signal();
//                    这种机制，是典型一个活动流程三段机制，分在多线程环境运行
//                    Ａ线程事件前的代码也就是引子是第一段，Ｂ线程完全运行回调函数是第二段，
//                    Ａ线程事件后的代码是第三段，然后开始下一次引子
//                    这种代码跟单线程运行效果完全一致，不会有信息丢失，但是比单线程还慢
//                    因为段与段之间是有空隙的．所以目前没找到它的意义．
                    meimeitag.await();
                    debug("等待执行回调函数执行完毕");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    meimeilock.unlock();
                }


            }


        },"meimei").start();
        new Thread(()-> {
            CloneMeimei cloneMeimei = null;
            while (true){
                meimeilock.lock();
                try{
                    meimeitag.await();
                    //回调函数
                    //xiaoming_age=meimei_age+8;
                    cloneMeimei = new CloneMeimei(syncMeiMei);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    meimeilock.unlock();
                }
                update(cloneMeimei);
                debug("小明年龄　"+syncXiaoMing.getAge());
                meimeilock.lock();
                try {
                    meimeitag.signal();
                }finally {
                    meimeilock.unlock();
                }
            }


        },"xiaoming").start();

        while (true){
            Thread.sleep(2000);
            System.out.println();
            meimeilock.lock();
            try{
                debug(syncMeiMei);

            }finally {
                meimeilock.unlock();
            }
        }

//        System.out.println("end");
    }
    public static void update(CloneMeimei meimei){
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        syncXiaoMing.setAge(meimei.getAge()+8);
    }
}
class CloneMeimei{
    public CloneMeimei(SyncMeiMei meiMei) {
        this.age = meiMei.getAge();
    }

    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
